#!/bin/bash
# - rpm_name
# - do_remove_rpms

. $LKP_SRC/lib/debug.sh
. $LKP_SRC/lib/upload.sh
. $LKP_SRC/lib/log.sh
. $LKP_SRC/lib/rpm.sh

. $LKP_SRC/lib/job.sh
load_job_vars result_root

: "${DISTRO:=openeuler}"
: "${basearch:=$(arch)}"

. $LKP_SRC/distro/common
load_distro_funcs $DISTRO

[ -n "${rpm_name}" ]		|| die "rpm_name is empty"

rpm_name=${rpm_name//,/ }
rpm_name=($rpm_name)

echo_rpm_location()
{
        for i in "${rpm_name[@]}"
        do
                echo "Starting print rpm: ${i} location"
                repoquery --location $i 2>/dev/null | grep -v src.rpm
                echo "print rpm: ${i} location end"
        done
}

echo_rpm_evr()
{
        for i in "${rpm_name[@]}"
        do
                echo "Starting print rpm: ${i} evr"
                repoquery --queryformat '%{evr}' $i 2>/dev/null
                echo "print rpm: ${i} evr end"
        done

}

echo_src_rpm_location()
{
        for i in "${rpm_name[@]}"
        do
                echo "Starting print rpm: ${i}'s src.rpm location"
                src_rpm_name=$(repoquery --queryformat '%{source_name}' $i 2>/dev/null)

		[ -n "$src_rpm_name" ] && {
			repoquery --location $src_rpm_name 2>/dev/null | grep src.rpm
		}
                echo "print rpm: ${i}'s src.rpm location end"
        done
}

echo_rpm_license()
{
        for i in "${rpm_name[@]}"
        do
                echo "Starting print rpm: ${i} license"
                repoquery --qf '%{license}' $i 2>/dev/null
                echo "print rpm: ${i} license end"
        done
}

install_rpm()
{
	log_info "Starting install test: yum install -y ${rpm_name[@]}"
	yum install -y "${rpm_name[@]}" 2>&1 || die "install rpm failed"
	echo "install success: ${rpm_name[@]}"
}

install_rpms()
{
	local installed_rpms=()

	log_info "Starting install test: yum install -y ${rpm_name[@]}"
	for i in "${rpm_name[@]}"
	do
		if [ -n "$pr_merge_reference_name" ]; then
			cp /home/lkp/rpmbuild/RPMS/*/* .
			echo "begin to yum localinstall rpm"
			a=$(yum localinstall -y *.rpm 2>&1)
		else
			a=$(yum install -y "$i" 2>&1)
		fi
		echo "$a"
		echo "$a" | grep -q "Complete!"
		if [ $? -eq 0 ]; then
			echo "rpm install result, rpm name: $i, state: pass"
			echo "$a" | grep -q "Nothing to do." && continue
			local index=${#installed_rpms[@]}
			installed_rpms[$index]=$i
		else
			echo "rpm install result, rpm name: $i, state: fail"
		fi
	done

	rpm_name=("${installed_rpms[@]}")
}

query_group()
{
	for i in "${rpm_name[@]}"
	do
		local group=$(repoquery --installed --qf '%{group}' $i | head -n 2 | tail -n 1)
		echo "query group result, rpm name: $i, group: $group"

	done
}

query_libs()
{
	for i in "${rpm_name[@]}"
	do
		repoquery -l $i | grep -E "\.so$|\.a$"
		if [ $? -eq 0 ]; then
			echo "query libs result, rpm name: $i, libs: true"
		else
			echo "query libs result, rpm name: $i, libs: false"
		fi
	done
}

run_cmd()
{
	log_info "Starting run cmd"
	for i in "${rpm_name[@]}"
	do
		local cmd_list=($(rpm -ql $i | grep -E '/s?bin/'))
                [ -z "${cmd_list}" ] || {
                        for j in "${cmd_list[@]}"
                        do
				strings $j | grep -- --help >/dev/null 2>&1
				if [ $? -eq 0 ]; then
					timeout 5 $j --help >/dev/null 2>&1
					if [ $? -eq 0 ]; then
						echo "run cmd help result, rpm name: $i, cmd: $j, state: pass"
					else
						echo "run cmd help result, rpm name: $i, cmd: $j, state: fail"
					fi
				fi
                        done
                }
        done
}

run_service()
{
	log_info "Starting run service start/status/stop"
	for i in "${rpm_name[@]}"
	do
		local service_list=($(rpm -ql $i | grep -E "\.service$" | awk -F "/" '{print $NF}'))
                [ -z "${service_list}" ] || {
                        for j in "${service_list[@]}"
                        do
				for k in "start" "status" "stop"
				do
					timeout 10 systemctl ${k} ${j} >/dev/null 2>&1
					if [ $? -ne 0 ]; then
						echo "run service action result, rpm name: $i, service: $j, action: $k, state: fail"
					else
						echo "run service action result, rpm name: $i, service: $j, action: $k, state: pass"
					fi
				done
                        done
                }
        done

}


ldd_test()
{
	log_info "Starting ldd test"
	for i in "${rpm_name[@]}"
	do
		local ldd_list=$(rpm -ql $i | awk -F'/' '$3 ~ /bin|sbin|lib|lib64/')
		[ -z "${ldd_list}" ] || {
			for i in "${ldd_list[@]}"
			do
				[[ -f "$i" && -x "$i" ]] && {
					echo "ldd for $i"
					ldd -d -r $i
				}
			done
		}
	done
}

remove_rpm()
{
	for i in "${rpm_name[@]}"
	do
		local rpm_full_name=$(repoquery --installed --queryformat '%{name}-%{evr}.%{arch}' $i 2>/dev/null)
		echo "start remove test: ${rpm_full_name}"
		yum remove -y "$i" 2>&1
		echo "remove test finished: ${rpm_full_name}"
	done
}

remove_rpms()
{
	log_info "starting remove test"
	for i in "${rpm_name[@]}"
	do
		local rpm_full_name=$(repoquery --installed --queryformat '%{name}-%{evr}.%{arch}' $i 2>/dev/null)
		yum remove -y "$i" 2>&1
		if [ $? -eq 0 ]; then
			echo "rpm uninstall result, rpm name: $i, state: pass"
		else
			echo "rpm uninstall result, rpm name: $i, state: fail"
		fi
	done
}

commit_comment()
{
        owner="src-oepkgs"
	repo_name=${upstream_repo##*/}
        [ $1 == "install-rpm_success" ] && result="SUCCESS" || result="FAILED"
	mount_repo_addr=($mount_repo_addr)
	last_mount_repo_addr=${mount_repo_addr[-1]}
	mount_repo=$(echo $last_mount_repo_addr | sed 's/:20018//')
	testing_repo="[testing_repo]($mount_repo/)"
	result_url="https://api.compass-ci.openeuler.org$result_root/output"
	pub_repo=$(echo $mount_repo | sed 's;https://api.compass-ci.openeuler.org/rpm/testing/openeuler;https://repo.oepkgs.net/openEuler/rpm/openEuler;')
	oepkgs_repo="[Oepkg_repo]($pub_repo/)"
	result_detail="[report]($result_url)"

	body="|Arch|Result|Result_Detail|Testing_repo|Oepkgs_repo|\n|---|---|---|---|---|\n|$(arch)|$result|$result_detail|$testing_repo|$oepkgs_repo|"
	data='{"access_token": "'$secrets_my_token'", "body": "'$body'"}'
	curl -sS --header 'Content-Type: application/json;charset=UTF-8' -XPOST "https://gitee.com/api/v5/repos/$owner/$repo_name/commits/$upstream_commit/comments" -d "${data}" > /dev/null
}

die()
{
	[ -n "$upstream_commit" ] && [ -n "$secrets_my_token" ] && commit_comment "install-rpm_failed"
	echo "$@"
	exit 99
}


[ -n "${pr_merge_reference_name}" ] || {
	distro_install_depends install-rpm
	add_repo
	echo_rpm_location
	echo_rpm_evr
	echo_src_rpm_location
	echo_rpm_license
}

install_rpms
[ -n "${pr_merge_reference_name}" ] || {
	query_group
	query_libs
}
run_cmd
run_service
ldd_test

[ -n "${do_remove_rpms}" ] && {
	remove_rpms
}

[ -n "$upstream_commit" ] && [ -n "$secrets_my_token" ] && commit_comment "install-rpm_success"
